package io.dshubs.archives.controller;


import io.dshubs.archives.entity.ArchiveBaseInfo;
import io.dshubs.archives.entity.ArchiveUsingRecords;
import io.dshubs.archives.service.ArchiveBaseInfoService;
import io.dshubs.archives.service.ArchiveUsingRecordsService;
import io.dshubs.commons.log.annotation.LogOperation;
import io.dshubs.commons.tools.constant.Constant;
import io.dshubs.commons.tools.exception.RenException;
import io.dshubs.commons.tools.page.PageData;
import io.dshubs.commons.tools.utils.Result;
import io.dshubs.commons.tools.validator.ValidatorUtils;
import io.dshubs.commons.tools.validator.group.AddGroup;
import io.dshubs.commons.tools.validator.group.DefaultGroup;
import io.dshubs.commons.tools.validator.group.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 档案利用记录表 前端控制器
 * </p>
 *
 * @author tan
 * @since 2021-08-27
 */
@Api(tags = "档案利用记录")
@RestController
@RequestMapping("/using-records")
public class ArchiveUsingRecordsController {
    @Autowired
    private ArchiveUsingRecordsService archiveUsingRecordsService;

    @GetMapping("page")
    @ApiOperation("档案利用:page")
    @LogOperation("档案利用:/archive/using-records:/img/icon-档案利用.png:page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<PageData<ArchiveUsingRecords>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        return new Result<PageData<ArchiveUsingRecords>>().ok(archiveUsingRecordsService.page(params));
    }


    @GetMapping("{id}")
    @ApiOperation("详情")
    @LogOperation("档案利用:get")
    public Result<ArchiveUsingRecords> get(@PathVariable("id") Long id){
        return new Result<ArchiveUsingRecords>().ok(archiveUsingRecordsService.selectById(id));
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("档案利用:save")
    public Result save(@RequestBody ArchiveUsingRecords archiveUsingRecords){
        List<ArchiveUsingRecords> archiveUsingRecordList = new ArrayList<>();
        ValidatorUtils.validateEntity(archiveUsingRecords, AddGroup.class, DefaultGroup.class);
        List<String> fileNumberList = archiveUsingRecords.getFileNumberList();
        if(CollectionUtils.isNotEmpty(fileNumberList)) {
            fileNumberList.forEach(i->{
                archiveUsingRecords.setApplyDate(new Date());
                archiveUsingRecords.setFileNumber(i);

                archiveUsingRecordList.add(archiveUsingRecords);
            });
            archiveUsingRecordsService.insertBatch(archiveUsingRecordList);
        } else {
            throw new RenException("未选择利用文件");
        }


        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("档案利用:update")
    public Result update(@RequestBody ArchiveUsingRecords archiveUsingRecords){
        //效验数据
        ValidatorUtils.validateEntity(archiveUsingRecords, UpdateGroup.class, DefaultGroup.class);

        archiveUsingRecordsService.updateById(archiveUsingRecords);

        return new Result();
    }

    @DeleteMapping()
    @ApiOperation("删除")
    @LogOperation("档案利用:delete")
    @PreAuthorize("hasAuthority('archive:info:delete')")
    public Result delete(@RequestBody @NotNull(message = "批量删除id不能为空") List<Long> ids){
        archiveUsingRecordsService.deleteBatchIds(ids);
        return new Result();
    }


    @PutMapping("/give-back")
    @LogOperation("档案利用:giveBack")
    public Result giveBack(@RequestBody @NotNull(message = "归还id不能为空") List<String> ids){
        archiveUsingRecordsService.giveBack(ids);
        return new Result();
    }

}
